package com.ssd.controller;

import com.ssd.entity.RegionEntity;
import com.ssd.entity.RegionEntity;
import com.ssd.service.RegionService;
import com.ssd.utils.StringUtils;
import com.ssd.view.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/region")
@Slf4j
public class RegionController {

    @Resource
    private RegionService regionService;

    // 分页查询商品列表
    @GetMapping("/list")
    public BaseResponse<List<RegionEntity>> getRegionPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10000") int pageSize,
            @RequestParam(required = false) String name) {
        return new BaseResponse(regionService.getRegionList(pageNum, pageSize,name));
    }

    /**
     * 根据主键获取商品详情
     */
    @GetMapping("/{regionId}")
    public BaseResponse<RegionEntity> getVMDetail(@PathVariable("regionId") Long regionId) {
        return new BaseResponse(regionService.selectRegionById(regionId));
    }

    /**
     * 新增商品信息
     */
    @PostMapping
    public BaseResponse<String> addVM(@RequestBody RegionEntity region) {
        return new BaseResponse(regionService.insertRegion(region));
    }

    /**
     * 修改商品信息
     */
    @PutMapping
    public BaseResponse<String> updateVM(@RequestBody RegionEntity region) {
        return new BaseResponse(regionService.updateRegion(region));
    }

    /**
     * 批量删除商品信息
     */
    @DeleteMapping("/{regionIds}")
    public BaseResponse<String> batchDeleteVM(@PathVariable("regionIds") String classIdsStr) {
        Long[] classIds = StringUtils.isEmpty(classIdsStr) ?
                new Long[0] :
                Arrays.stream(classIdsStr.split(","))
                        .map(Long::parseLong)
                        .toArray(Long[]::new);
        return new BaseResponse(regionService.deleteRegionByRegionIds(classIds));
    }
}